Do ALL your variables need to be declared private? [closed]

Posted by skizeey on Programmers See other posts from Programmers or by skizeey
Published on 2011-03-13T08:24:00Z Indexed on 2012/04/12 5:40 UTC
Read the original article Hit count: 211

Filed under:

Possible Duplicate:
Why do we need private variables?

I know that it's best practice to stay safe, and that we should always prevent others from directly accessing a class' properties. I hear this all the time from university professors, and I also see this all the time in a lot of source code released on the App Hub. In fact, professors say that they will actually take marks off for every variable that gets declared public.

Now, this leaves me always declaring variables as private. No matter what. Even if each of these variables were to have both a getter and a setter.

But here's the problem: it's tedious work. I tend to quickly lose interest in a project every time I need to have a variable in a class that could have simply been declared public instead of private with a getter and a setter.

So my question is, do I really need to declare all my variables private? Or could I declare some variables public whenever they require both a getter and a setter?

© Programmers or respective owner

Related posts about code-quality